; FDISK - Format Disk for POSx86
; This formats the current drive 
; WARNING : USE WITH CARE
bits 16
%include "system.inc"
org 8000h
db 'NT'
start:
	call os_clear_screen
	mov ax, .list
	mov bx, .warn
	mov cx, .warn2
	call os_list_dialog
	jc .esc_pressed
	cmp ax,1
	je .esc_pressed
	cmp ax,2
	je .format_ok
	.list db 'No,Yes',0
	.warn db 'WARNING : FORMATTING WILL ERASE ALL THE DATA ON THIS DISK!',13,10,0
	.warn2 db 'ARE YOU SURE [Y/N] : ',13,10,0
.esc_pressed:
	ret
	
.format_ok:
	call os_clear_screen
	mov si,formatting
	call os_print_string
	mov ax,40
	call os_pause
	mov ax,19
	call disk_convert_l2hts
	mov [.track], al
	mov [.head],dh
	mov ah,0x05
	mov dh,[.head]
	mov ch,[.track]
	mov al,14
	mov dl,0x0
	int 0x13
	jc os_fatal_error
	cmp ah,0x0
	je .done
	.track dw 0
	.head dw 0
.done:
	mov si,msg
	call os_print_string
	ret
	
disk_convert_l2hts:
	push bx
	push ax

	mov bx, ax			; Save logical sector

	mov dx, 0			; First the sector
	div word [SecsPerTrack]		; Sectors per track
	add dl, 01h			; Physical sectors start at 1
	mov cl, dl			; Sectors belong in CL for int 13h
	mov ax, bx

	mov dx, 0			; Now calculate the head
	div word [SecsPerTrack]		; Sectors per track
	mov dx, 0
	div word [Sides]		; Floppy sides
	mov dh, dl			; Head/side
	mov ch, al			; Track

	pop ax
	pop bx

; ******************************************************************
	mov dl, [bootdev]		; Set correct device
; ******************************************************************
	ret

	Sides dw 2
	SecsPerTrack dw 18
; ******************************************************************
	bootdev db 0			; Boot device number
; ******************************************************************
	msg db 'Format was a success!',0
	formatting db 'Formatting IBM-PC 3.5"" Floppy Disk....',13,10,0